package com.tange.ai.iot.client.gnssa.internal

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.util.Consumer
import com.blankj.utilcode.util.ToastUtils
import com.gyf.immersionbar.ImmersionBar
import com.tange.ai.iot.client.gnssa.R
import com.tange.ai.iot.client.gnssa.configure.AppSettingsActivity
import com.tange.ai.iot.client.gnssa.databinding.ActivityInternalDispatcherBinding
import com.tange.ai.iot.client.gnssa.home.HomeActivity
import com.tange.ai.iot.client.gnssa.lan.LanConnectWaysActivity
import com.tange.ai.iot.client.gnssa.token.InternalAccountLoginActivity
import com.tange.ai.iot.client.gnssa.token.XqAccountLoginActivity
import com.tange.base.toolkit.GlobalApplicationContext
import com.tange.base.toolkit.PreferenceUtil
import com.tange.base.toolkit.onClicked
import com.tange.core.access.token.Authentication.configure
import com.tange.core.access.token.Token
import com.tange.core.backend.service.interceptor.HttpLoggingInterceptor
import com.tange.core.data.structure.Resp

class DispatchActivity: AppCompatActivity() {

    private lateinit var binding: ActivityInternalDispatcherBinding

    override fun onResume() {
        super.onResume()
//        startActivity(Intent(applicationContext, XqAccountLoginActivity::class.java))
//        if (!TextUtils.isEmpty(PreferenceUtil.getString(this, InternalAccountLoginActivity.PRE_TOKEN))) {
//            configure(PreferenceUtil.getString(this, InternalAccountLoginActivity.PRE_TOKEN),
//                Consumer<Resp<Token?>> { tokenResp ->
//                    if (tokenResp.success && tokenResp.data != null) {
//                        startActivity(Intent(this, XqAccountLoginActivity::class.java))
//                    } else {
//                        ToastUtils.showShort("用户令牌校验失败")
//                        startActivity(Intent(this, XqAccountLoginActivity::class.java))
//                    }
//                })
//        }else{
//            startActivity(Intent(applicationContext, XqAccountLoginActivity::class.java))
//        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ImmersionBar.with(this).statusBarColor(R.color.white).statusBarDarkFont(true).init();

        HttpLoggingInterceptor.enableLogging = true

        binding = ActivityInternalDispatcherBinding.inflate(layoutInflater).apply {
            setContentView(root)
            supportActionBar?.hide()

            appConfigure.onClicked {
                startActivity(Intent(this@DispatchActivity, AppSettingsActivity::class.java))
            }

            lanConnect.onClicked {
                startActivity(Intent(this@DispatchActivity, LanConnectWaysActivity::class.java))
            }

            usingAccountGnssa.onClicked {
                startActivity(Intent(it.context, InternalAccountLoginActivity::class.java))
            }

            usingXqLogin.onClicked {
                startActivity(Intent(it.context, XqAccountLoginActivity::class.java))
            }

            usingAccount.onClicked {
                startActivity(Intent(it.context, InternalAccountLoginActivity::class.java))
            }

            scanToken.onClicked {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ActivityCompat.checkSelfPermission(
                        GlobalApplicationContext.application(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
                ) {
                    requestPermissions(arrayOf(Manifest.permission.CAMERA), 100)
                } else {
                    startActivity(Intent(it.context, TokenQrcodeScanActivity::class.java))
                }
            }

            scanQrcode.onClicked {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ActivityCompat.checkSelfPermission(
                        GlobalApplicationContext.application(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
                ) {
                    requestPermissions(arrayOf(Manifest.permission.CAMERA), 100)
                } else {
                    startActivity(Intent(it.context, DebuggingQrcodeScanActivity::class.java))
                }
            }
        }

        window.apply {
            var uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            uiFlags = uiFlags or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            statusBarColor = Color.TRANSPARENT
            decorView.systemUiVisibility = uiFlags
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            }
        }

    }
}